import { _decorator, Camera, Component, game, renderer, Node, Rect, UITransform, Vec3, view, Widget } from 'cc';
import { UIRootController } from './UIRootController';
const { ccclass, property, requireComponent } = _decorator;

@ccclass('UIRootSizeMather')
@requireComponent(UITransform)
export class UIRootSizeMather extends Component {
    @property
    private matchWidth: boolean = true;

    @property
    private matchHeight: boolean = true;

    private _uiTransform: Camera = null!;

    protected onLoad(): void {
        this._uiTransform = this.node.getComponent(UITransform)!;
        game.on(UIRootController.Event.ROOT_SIZE_CHANGED, this._onRootSizeChanged, this);
        this._onRootSizeChanged();
    }

    protected onDestroy(): void {
        game.off(UIRootController.Event.ROOT_SIZE_CHANGED, this._onRootSizeChanged, this);
    }

    private _onRootSizeChanged(): void {
        const inst = UIRootController.instance;
        if (!inst || !inst.isRunning) return;
        if (this.matchWidth) {
            this._uiTransform.width = inst.uiSize.width;
        }
        if (this.matchHeight) {
            this._uiTransform.height = inst.uiSize.height;
        }
    }
}


